package com.c2.web.backend;


import com.c2.entity.EbUser;
import com.c2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 将数据放到 model中，将页面放到view中
     * spring mvc---将model渲染到view中
     *
     * list ---分页请求数据
     */
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1") int psize,
    @RequestParam(value = "pageSize",defaultValue = "5") int pageSize)
    {
        System.out.println("start service.....;psize==>"+psize);
        //1.请求service层获取新闻的分页数据
        List<EbUser> userList = userService.listByPage(psize, 3);
        int totalPage = userService.getMaxPage(pageSize);
        //2.传递model(数据)和view视图(jsp)
        model.addAttribute("userList",userList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("curPage",psize);
        return "manage/user";//视图  通过manage/news.jsp进行展示数据
    }
    @RequestMapping("/to_save")
    public  String toSave(){
        return "manage/user-add";
    }
    @RequestMapping("/save_commit")
    public String saveCommit(EbUser ebUser){
        if (ebUser.getEuUserId()==null||ebUser.getEuUserId().trim().length()==0){
            return "manage/paramError";
        }
        int result = userService.save(ebUser);
        if(result<=0){
            return "manage/paramError";
        }
        return "manage/manage-result-user";
    }
    @RequestMapping("/delete/{euUserId}")
    public String delete(@PathVariable("euUserId") String euUserId){
        int result = userService.deleteByPk(euUserId);
        return "manage/manage-result-user";
    }
    @RequestMapping("/edit")
    public String edit(Model model, String euUserId){
         EbUser ebUser = userService.select(euUserId);
        model.addAttribute("ebUser",ebUser);
        return "manage/user-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbUser ebUser){
        System.out.println(ebUser);
        int result = userService.updateById(ebUser);
        if (result <= 0){
            return "manage/manage-error-user";
        }
        return "manage/manage-result-user";
    }





    }

